# Maintainer:  Jason Kercher <jkercher 43 at gmail dot com>

pkgname=linuxcnc-git
pkgver=2.9.7.r3560.ga53456cf23
pkgrel=1
pkgdesc="Controls CNC machines. It can drive milling machines, lathes, 3d printers, laser cutters, plasma cutters, robot arms, hexapods, and more (formerly EMC2)"
arch=($CARCH)
license=('GPL-2.0-only')
url="http://linuxcnc.org"
depends=(
  at-spi2-core
  cairo
  bash
  boost-libs
  freetype2
  fontconfig
  gcc-libs
  gdk-pixbuf2
  glib2
  glibc
  gstreamer
  gtk3
  harfbuzz
  libepoxy
  libmodbus
  #   libgpiod
  librsvg
  libtirpc
  libusb
  libx11
  libxext
  libxft
  libxinerama
  libxmu
  libxss
  #   readline
  #   editline
  systemd-libs
  pango
  python
  python-cairo
  python-configobj
  python-dbus
  python-distro
  python-gobject
  python-opencv
  python-opengl
  python-numpy
  python-matplotlib
  #   python-poppler-qt5
  #   python-psycopg
  #   python-pillow
  python-pyqt5
  python-pyserial
  python-pyzmq
  python-xlib
  #   python-validate-pyproject
  readline
  tk
  tcl
  zlib
  # AUR
  bwidget
  python-yapps2
  #   gtksourceview2
  #   python-pyodbc
  # python-espeak
)
makedepends=(
  asciidoc
  bc
  boost
  dblatex
  gettext
  glu
  git
  libeatmydata
  libxaw
  libxslt
  intltool
  openbsd-netcat
  procps-ng
  psmisc
  python-pillow
  python-sphinx
  man-db
  texlive-bin
  texlive-binextra
  #AUR
  #   bwidget
  #   tclx
  linkchecker
  termcap
)
checkdepends=()
provides=('linuxcnc')
conflicts=('linuxcnc' 'linuxcnc-bin')
options=(!emptydirs)
source=("${pkgname}::git+https://github.com/LinuxCNC/linuxcnc"
  'libtirpc.patch')
sha256sums=('SKIP'
  'bc95bafd67fad1c1d3722261bc586cdc612ec9e1597fadb95fa825c10550ac2c')

pkgver() {
  cd "${srcdir}/${pkgname}"
  # Generate git tag based version. Count only proper (v)#.#* [#=number] tags.
  local _gitversion=$(git describe --long --tags --match '[v0-9][0-9.][0-9.]*' | sed -e 's|^v||' | tr '[:upper:]' '[:lower:]')

  # Format git-based version for pkgver
  # Expected format: e.g. 1.5.0rc2.r521.g99982a1c
  echo "${_gitversion}" | sed \
    -e 's|^\([0-9][0-9.]*\)-\([a-zA-Z]\+\)|\1\2|' \
    -e 's|\([0-9]\+-g\)|r\1|' \
    -e 's|-|.|g'
}

prepare() {
  git -C "${srcdir}/${pkgname}" clean -dfx

  cd "${srcdir}/${pkgname}/src"
  echo "export TCLLIBPATH=$TCLLIBPATH:/usr/lib/tcltk/linuxcnc" >${pkgname}.sh
  find . -iname fixpaths.py -o -iname checkglade -o \
    -iname update_ini | xargs perl -p -i -e "s/python/python3/"
  patch -Np2 -i "${srcdir}/libtirpc.patch"
  sed -i 's|/usr/local/etc/emc2/configs|/etc/emc2/configs|g' Makefile.inc.in
  sed -i 's|$(DESTDIR)$(sysconfdir)/linuxcnc|$(DESTDIR)/etc/linuxcnc|g' Makefile

  autoreconf -i
  eatmydata ./autogen.sh
  eatmydata ./configure \
    --prefix=/usr \
    --disable-check-runtime-deps \
    --enable-non-distributable=yes \
    --enable-build-documentation \
    --with-realtime=uspace
}

build() {
  cd "${srcdir}/${pkgname}/src"
  #   make
  eatmydata make -O -j$((1 + $(nproc))) default pycheck V=1
  # Note that the package build covers html docs
  eatmydata make -O -j$((1 + $(nproc))) manpages V=1
  #   eatmydata make -O -j$((1 + $(nproc))) translateddocs V=1
  eatmydata make -O -j$((1 + $(nproc))) default pycheck V=1
}

# check() {
#   cd "${srcdir}/${pkgname}/src"
#   ../scripts/rip-environment runtests
# }

package() {
  cd "${srcdir}/${pkgname}/src"
  DESTDIR=${pkgdir} make install
  cp -PR "${srcdir}/${pkgname}/share/applications" "${pkgdir}/usr/share"
  mkdir -p "${pkgdir}/etc/xdg"
  cp -PR "${srcdir}/${pkgname}/share/menus" "${pkgdir}/etc/xdg/"
  install -Dm755 "${srcdir}/${pkgname}/src/${pkgname}.sh" \
    "${pkgdir}/etc/profile.d/${pkgname}.sh"
  sed -i "s|${srcdir}||" "${pkgdir}/usr/share/linuxcnc/Makefile.modinc"
  install -Dm644 ${pkgdir}/usr/share/linuxcnc/linuxcncicon.png -t ${pkgdir}/usr/share/pixmaps
}
